﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;
using MvcAtletIS.Helpers;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class WebcontactsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        //
        // GET: /Admin/Webcontacts/

        public ViewResult Index(int CategoryId)
        {
            ViewBag.ParentCategory = db.WebcontactsCategories.SingleOrDefault(lc => lc.ID == CategoryId);
            var webcontacts = db.Webcontacts.Include("WebcontactsCategories").Where(wc => wc.CategoryId == CategoryId).OrderBy(wc => wc.Order).ThenBy(wc => wc.Surname);
            return View(webcontacts.ToList());
        }

        //
        // GET: /Admin/Webcontacts/Details/5

        public ViewResult Details(int id)
        {
            Webcontacts webcontacts = db.Webcontacts.Single(w => w.ID == id);
            return View(webcontacts);
        }

        //
        // GET: /Admin/Webcontacts/Create

        public ActionResult Create(int CategoryId)
        {
            Webcontacts model = new Webcontacts();
            model.CategoryId = CategoryId;
            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/contacts"), string.Empty);
            return View(model);
        } 

        //
        // POST: /Admin/Webcontacts/Create

        [HttpPost]
        public ActionResult Create(Webcontacts webcontacts)
        {
            if (ModelState.IsValid)
            {
                db.Webcontacts.AddObject(webcontacts);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = webcontacts.CategoryId });  
            }

            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/contacts"), string.Empty);
            return View(webcontacts);
        }
        
        //
        // GET: /Admin/Webcontacts/Edit/5
 
        public ActionResult Edit(int id)
        {
            Webcontacts webcontacts = db.Webcontacts.Single(w => w.ID == id);
            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/contacts"), string.Empty);
            return View(webcontacts);
        }

        //
        // POST: /Admin/Webcontacts/Edit/5

        [HttpPost]
        public ActionResult Edit(Webcontacts webcontacts)
        {
            if (ModelState.IsValid)
            {
                db.Webcontacts.Attach(webcontacts);
                db.ObjectStateManager.ChangeObjectState(webcontacts, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = webcontacts.CategoryId });
            }

            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/contacts"), string.Empty);
            return View(webcontacts);
        }

        //
        // GET: /Admin/Webcontacts/Delete/5
 
        public ActionResult Delete(int id)
        {
            Webcontacts webcontacts = db.Webcontacts.Single(w => w.ID == id);
            return View(webcontacts);
        }

        //
        // POST: /Admin/Webcontacts/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Webcontacts webcontacts = db.Webcontacts.Single(w => w.ID == id);
            int CategoryId = Convert.ToInt32(webcontacts.CategoryId);
            db.Webcontacts.DeleteObject(webcontacts);
            db.SaveChanges();
            return RedirectToAction("Index", new { CategoryId = CategoryId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}